package top.jacktgq.hsp.netty.tcp_selfprotocal;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @Author CandyWall
 * @Date 2020/11/13--17:59
 * @Description 自定义MessageProtocal解码器
 */
public class MyMessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyMessageDecoder的decode()方法被调用");
        //需要将得到的二进制字节码 -> MessageProtocal 数据包（对象）
        int len = in.readInt();
        byte[] buffer = new byte[len];
        in.readBytes(buffer);
        //封装成Message对象
        MessageProtocal messageProtocal = new MessageProtocal(len, buffer);
        out.add(messageProtocal);
    }
}
